//
//  NetworkState.m
//  Eyess
//
//  Created by lanou on 15/11/3.
//  Copyright © 2015年 追随我心. All rights reserved.
//

#import "NetworkState.h"


@implementation NetworkState
+ (NetworkState *)shareInstance
{
    static NetworkState *net = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        net = [[NetworkState alloc] init];
        
    });
    return net;
    
}
- (NetworkStatus)reachablity
{
    Reachability *readablity = [Reachability reachabilityWithHostName:@"www.baidu.com"];
    
    switch ([readablity currentReachabilityStatus])
    {
        case NotReachable:
           
            return NotReachable;
            break;
        case ReachableViaWiFi:
           
            return ReachableViaWiFi;
            break;
        case ReachableViaWWAN:
            
            return ReachableViaWWAN;
            break;
            
        
    }
}

- (void)reachablityStates:(UIViewController *)vc;
{
    
    
    [MBProgressHUD hideHUDForView:vc.view animated:YES];
    
    MBProgressHUD *mbp = [MBProgressHUD showHUDAddedTo:vc.view animated:YES];
    
    mbp.labelText = @"Loading...";

    NetworkStatus stats = [[NetworkState shareInstance] reachablity];
    if (stats == NotReachable)
    {
        [MBProgressHUD hideHUDForView:vc.view animated:YES];

        return;
        
    }
    else if (stats == ReachableViaWWAN)
    {
        if ([[NSUserDefaults standardUserDefaults]boolForKey:StrUsertrafficToRemind])//此处判断是否有此项设置
        {
            UIAlertController *aler = [UIAlertController alertControllerWithTitle:@"提示" message:@"现在使用的是数据网络,是否取消请求" preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *action = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
            UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                [MBProgressHUD hideHUDForView:vc.view animated:YES];
                return ;
            }];
            
            [aler addAction:action];
            [aler addAction:sureAction];
            [vc presentViewController:aler animated:YES completion:nil];
            
        }
        
        
    }

}

- (void)reachablityStatesWithNoJuhua:(UIViewController *)vc;
{
    
    
    
    NetworkStatus stats = [[NetworkState shareInstance] reachablity];
    if (stats == NotReachable)
    {
        
        
        return;
        
    }
    else if (stats == ReachableViaWWAN)
    {
        if ([[NSUserDefaults standardUserDefaults]boolForKey:StrUsertrafficToRemind])//此处判断是否有此项设置
        {
            UIAlertController *aler = [UIAlertController alertControllerWithTitle:@"提示" message:@"现在使用的是数据网络,是否取消请求" preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *action = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
            UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                
                return ;
            }];
            
            [aler addAction:action];
            [aler addAction:sureAction];
            [vc presentViewController:aler animated:YES completion:nil];
            
        }
        
        
    }
    
}



- (void)alerAction:(UIAlertController *)aler
{
    [aler dismissViewControllerAnimated:YES completion:nil];
}
@end
